/*
 * test_framework_database.h
 *
 *  Created on: 2014. 7. 1.
 *      Author: jjaehuny
 */

#ifndef TEST_FRAMEWORK_DATABASE_H_
#define TEST_FRAMEWORK_DATABASE_H_

#include <memory>

#include <gtest/gtest.h>
#include <glog/logging.h>

#include <Framework/Database/Connector.h>
#include <Framework/Database/Command.h>

void OnResponse(std::shared_ptr<::Framework::Database::Command> command)
{
	if (false == command->get_isSuccess_())
		return;

	while (command->get_resultSet_()->next())
	{
		for (int index = 1; index < 7; index++)
		{
			LOG(INFO) << "[col " << index << ": " << command->get_resultSet_()->getString(index) << "]";
		}
	}
}

class test_framework_database : public testing::Test
{
public:
	class Dummy
	{
	public:
		Dummy() { LOG(INFO) << __PRETTY_FUNCTION__; }
		~Dummy() { LOG(INFO) << __PRETTY_FUNCTION__; }
	};

public:

	// Sets up the test fixture.
	virtual void SetUp() {
		::Framework::Database::Connector * pConnector = new ::Framework::Database::Connector;
		::Framework::Database::Connector::Param clsParam;
		clsParam.type_ = 1;
		clsParam.count_ = 2;
		clsParam.host_ = "172.16.3.111";
		clsParam.user_ = "gamevil";
		clsParam.pass_ = "vlwlrpaqlf";
		clsParam.name_ = "pz_user";

		bool bRetConnect = pConnector->Connect(clsParam);
		EXPECT_TRUE(bRetConnect);

		LOG(INFO) << __PRETTY_FUNCTION__;
	}

	// Tears down the test fixture.
	virtual void TearDown() {}
};

TEST_F(test_framework_database, test_framework_database_syncexecute)
{
	{
		::Framework::Database::Connector * pExecuter = ::Framework::Database::get_connector(1);
		EXPECT_TRUE(NULL != pExecuter);


		std::shared_ptr<::Framework::Database::Command> command = ::Framework::Database::create_command_singlethread();

		std::string str("SELECT * FROM env_config");
		//command->set_query_(str);
		command->MakeQuery("SELECT", " * FROM env_config");

		pExecuter->Sync_Execute(command);

		if (false == command->get_isSuccess_())
			return;


		while (command->get_resultSet_()->next())
		{
			for (int index = 1; index < 2; index++)
			{
				LOG(INFO) << "[col " << index << ": " << command->get_resultSet_()->getString(index) << "]";
			}
		}
	}
}

TEST_F(test_framework_database, test_framework_database_syncexecute_second)
{
	{
		::Framework::Database::Connector * pExecuter = ::Framework::Database::get_connector(1);
		EXPECT_TRUE(NULL != pExecuter);


		std::shared_ptr<::Framework::Database::Command> command = ::Framework::Database::create_command_singlethread();

		//std::string str("SELECT * FROM env_proxy_service");
		command->MakeQuery("SELECT ", "* FROM env_proxy_service");

		pExecuter->Sync_Execute(command);
		if (false == command->get_isSuccess_())
			return;

		while (command->get_resultSet_()->next())
		{
			for (int index = 1; index < 7; index++)
			{
				LOG(INFO) << "[col " << index << ": " << command->get_resultSet_()->getString(index) << "]";
			}
		}
	}
}


TEST_F(test_framework_database, test_framework_database_asyncexecute)
{
	{
		::Framework::Database::Connector * pExecuter = ::Framework::Database::get_connector(1);
		EXPECT_TRUE(NULL != pExecuter);


		std::shared_ptr<::Framework::Database::Command> command = ::Framework::Database::create_command_singlethread();

		//std::string str("SELECT * FROM env_proxy_service");
		command->MakeQuery("SELECT ", "* FROM env_database");
		command->set_OnResponse_(std::bind(OnResponse, std::placeholders::_1));

		pExecuter->Async_Execute(command->CAPTURE());
	}
}

TEST_F(test_framework_database, test_framework_database_asyncexecute_second)
{
	{
		::Framework::Database::Connector * pExecuter = ::Framework::Database::get_connector(1);
		EXPECT_TRUE(NULL != pExecuter);


		std::shared_ptr<::Framework::Database::Command> command = ::Framework::Database::create_command_singlethread();

		//std::string str("SELECT * FROM env_proxy_service");
		command->MakeQuery("SELECT ", "* FROM env_redis");
		command->set_OnResponse_(std::bind(OnResponse, std::placeholders::_1));

		pExecuter->Async_Execute(command->CAPTURE());
	}
}

#endif /* TEST_FRAMEWORK_DATABASE_H_ */
